/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace OptixDotNet 
{
	ref class Variable;

	
	/// <summary>
	/// Represents a compiled PTX CUDA kernal program. <br/>
	/// Types of Programs include: Ray generation, Exception, Closest Hit, Any Hit, Miss, Visit, Intersection, and Bounding.
    /// </summary>
	public ref class Program : public OptixNode, IVariableContainer
	{
	public:

		/// <summary>
		/// Creates a program located at [filename] with main function [programName]
        /// </summary>
		/// <param name="context">Context with which to create the program.</param>
		/// <param name="filename">Path of the compiled cuda ptx file holding the program.</param>
		/// <param name="programName">Name of the main function of the program.</param>
		Program( Context^ context, System::String^ filename, System::String^ programName );

		/// <summary>
		/// Checks to make sure the object is valid and consistent
        /// </summary>
		virtual void Validate() override
		{
			if( mProgram )
				CheckError( rtProgramValidate( mProgram ) );

			mProgram = 0;
		}

		/// <summary>
		/// Destroys the internal Optix resource
        /// </summary>
		virtual void Destroy() override
		{
			CheckError( rtProgramDestroy( mProgram ) );
		}

		/// <summary>
		/// Gets the number of global variables on the Program
        /// </summary>
		property int VariableCount
		{
			virtual int get()
			{
				unsigned int count;
				CheckError( rtProgramGetVariableCount( mProgram, &count ) );
				return count;
			}
		}

		/// <summary>
		/// Gets a local program variable with name [s]. If the variable doesn't exist, it is created.
        /// </summary>
		property Variable^ default[System::String^]
		{
			virtual Variable^ get( System::String^ s );
			virtual void set( System::String^s, Variable^ v );
		}

		/// <summary>
		/// Gets a local program variable at index [index]
        /// </summary>
		property Variable^ default[int]
		{
			virtual Variable^ get ( int index );
			virtual void set( int index, Variable^ v );
		}

	internal:
		Program( Context^ context, RTprogram program );
		inline RTprogram InternalPtr() { return mProgram; }

	protected:
		RTprogram mProgram;
	};
}